package com.it.emall.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.emall.domain.Recipes;
import com.it.emall.service.RecipesService;
import com.it.emall.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 食谱controller
 */
@RestController
@RequestMapping("/admin/recipes")
public class RecipesController {

    @Autowired
    private RecipesService recipesService;

    /**
     * 列表
     * @param recipesId
     * @param name
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public Object list(Integer recipesId, String name,@RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",required = false,defaultValue = "10") Integer limit )
    {
        PageHelper.startPage(page,limit);
        List<Recipes> list = recipesService.selectRecipesList(recipesId, name);
        PageInfo<Recipes> pageInfo = new PageInfo<>(list);
        //组织data数据
        Map<String,Object> data = new HashMap<>();
        data.put("list",pageInfo.getList());
        data.put("total",pageInfo.getTotal());
        data.put("pages",pageInfo.getPages());
        data.put("page",pageInfo.getPageNum());
        data.put("limit",pageInfo.getPageSize());
        return ResponseUtils.ok(data);
    }

    /**
     * 后台
     */
    @GetMapping("/manage/list")
    public Object selectlist(Integer recipesId, String name,@RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                             @RequestParam(value = "limit",required = false,defaultValue = "10") Integer limit){
        PageHelper.startPage(page, limit);
        List<Recipes> recipes = recipesService.selectManageRecipes();
        PageInfo<Recipes> pageInfo = new PageInfo<>(recipes);
        Map<String, Object> data = new HashMap<>();
        data.put("list", pageInfo.getList());
        data.put("total", pageInfo.getTotal());
        data.put("page", pageInfo.getPageNum());
        data.put("limit", pageInfo.getPageSize());
        return ResponseUtils.ok(data);
    }

    // 查询食谱（模糊查询 + 分页）
    @GetMapping("/manage/search")
    public Object list(
            @RequestParam(value = "recipeName", required = false) String recipeName,
            @RequestParam(value = "category", required = false) String category,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "instructions", required = false) String instructions,
            @RequestParam(value = "page",required = false,defaultValue = "1") int page,
            @RequestParam(value = "limit",required = false,defaultValue = "10") int limit
    ){
        PageHelper.startPage(page, limit);
        List<Recipes> recipes = recipesService.searchRecipes(recipeName, category, description, instructions);
        PageInfo<Recipes> pageInfo = new PageInfo<>(recipes);
        Map<String, Object> data = new HashMap<>();
        data.put("list", pageInfo.getList());
        data.put("total", pageInfo.getTotal());
        data.put("page", pageInfo.getPageNum());
        data.put("limit", pageInfo.getPageSize());
        return ResponseUtils.ok(data);
    }
    // 添加食谱
    @PostMapping("/manage/add")
    public Object add(@RequestBody Recipes recipe) {
        int i = recipesService.addRecipe(recipe);
        return ResponseUtils.ok(i);
    }
    // 修改食谱
    @PutMapping("/manage/update")
    public Object update(@RequestBody Recipes recipe) {
        int i = recipesService.updateRecipe(recipe);
        return ResponseUtils.ok(i);
    }
    // 删除食谱
    @DeleteMapping("/manage/delete")
    public Object delete(@RequestParam("id") Integer id) {
        int i = recipesService.deleteRecipe(id);
        return ResponseUtils.ok(i);
    }
}
